package com.zht.utils;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DesUtils {
    private static String key = "58563214";
    private static String key1 = "32128158";

    public DesUtils() {
    }

    public static String decrypt(String message) {
        try {
            System.out.println("decrypt:" + message);
            byte[] bytesrc = Base64.decodeBase64(message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key1.getBytes("UTF-8"));
            cipher.init(2, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public static String jHtdecrypt(String message) {
        try {
            byte[] bytesrc = Base64.decodeBase64(message);
            Cipher cipher = Cipher.getInstance("DES/ECB/noPadding");
            DESKeySpec desKeySpec = new DESKeySpec("EAF18968C7108ABC".getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(2, secretKey);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte, "UTF-8");
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static String idCardDec(String message) {
        String dec = decrypt(message);
        return dec != null ? dec : message;
    }

    public static String encrypt(String message) {
        try {
            System.out.println("encrypt:" + message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key1.getBytes("UTF-8"));
            cipher.init(1, secretKey, iv);
            return Base64.encodeBase64String(cipher.doFinal(message.getBytes("UTF-8")));
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }
}